package xfsale.service.impl.j2ee;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;

import xfsale.exception.AppException;

@SuppressWarnings("unused")
public final class PubPriv {

    private String serviceCode;			//功能代码
    private String acceptMethod;		//受理方式
    private String sysStatus;			//系统状态
    private String transAcctStatus;		//交易账户状态
    private String taAcctStatus;		//基金账户状态
    private String bankAcctStatus;		//银行账户状态
    private String fundStatus;			//基金状态
    private Connection conn;			//数据库连接
    private Logger logger = Logger.getLogger(PubAcct.class);	//日志

    public PubPriv(Connection conn) {
        this.conn = conn;
    }

    public void setServiceCode(String serviceCode) {
        this.serviceCode = serviceCode;
    }

    public void setAcceptMethod(String acceptMethod) {
        this.acceptMethod = acceptMethod;
    }

    public void setSysStatus(String sysStatus) {
        this.sysStatus = sysStatus;
    }

    public void setTransAcctStatus(String transAcctStatus) {
        this.transAcctStatus = transAcctStatus;
    }

    public void setTaAcctStatus(String taAcctStatus) {
        this.taAcctStatus = taAcctStatus;
    }

    public void setBankAcctStatus(String bankAcctStatus) {
        this.bankAcctStatus = bankAcctStatus;
    }

    public void setFundStatus(String fundStatus) {
        this.fundStatus = fundStatus;
    }

    //检查系统状态
    public void checkSys() throws Exception {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int count = 0;
        try {
            logger.info("检查系统状态是否允许执行...");
            stmt = conn.prepareStatement(""
                    + "select returnCode, returnMsg "
                    + "from ServiceSys "
                    + "where serviceCode = ? "
                    + "and sysStatus = ? ");
            stmt.setString(1, serviceCode);
            stmt.setString(2, sysStatus);
            rs = stmt.executeQuery();
            if (rs.next()) {
                String returnCode = rs.getString(1);
                String returnMsg = rs.getString(2);
                if (!returnCode.equals("0000")) {
                    throw new AppException(returnMsg);
                }
            }
            stmt.close();

            logger.debug("当前系统状态允许执行.");
        } catch (Exception e) {
            logger.error("检查系统状态出错. " + e);
            throw e;
        } finally {
            stmt.close();
        }
    }

    //检查交易帐号状态
    void checkAcctTrans() throws Exception {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int count = 0;
        try {
            logger.info("检查交易帐号状态是否允许执行...");
            stmt = conn.prepareStatement(""
                    + "select returnCode, returnMsg "
                    + "from ServiceAcct "
                    + "where serviceCode = ? "
                    + "and accountStatus = ? "
                    + "and accountType = '0' ");
            stmt.setString(1, serviceCode);
            stmt.setString(2, transAcctStatus);
            rs = stmt.executeQuery();
            if (rs.next()) {
                String returnCode = rs.getString(1);
                String returnMsg = rs.getString(2);
                if (!returnCode.equals("0000")) {
                    throw new AppException(returnMsg);
                }
            }
            stmt.close();
            logger.debug("当前交易帐号状态允许执行.");
        } catch (Exception e) {
            logger.error("检查交易帐号状态出错. " + e);
            throw e;
        } finally {
            stmt.close();
        }
    }

    //检查基金帐号状态
    public void checkAcctTA() throws Exception {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int count = 0;
        try {
            logger.info("检查基金帐号状态是否允许执行...");
            stmt = conn.prepareStatement(""
                    + "select returnCode, returnMsg "
                    + "from ServiceAcct "
                    + "where serviceCode = ? "
                    + "and accountStatus = ? "
                    + "and accountType = '1' ");
            stmt.setString(1, serviceCode);
            stmt.setString(2, taAcctStatus);
            rs = stmt.executeQuery();
            if (rs.next()) {
                String returnCode = rs.getString(1);
                String returnMsg = rs.getString(2);
                if (!returnCode.equals("0000")) {
                    throw new AppException(returnMsg);
                }
            }
            stmt.close();
            logger.debug("检查基金帐号状态成功.");
        } catch (Exception e) {
            logger.error("检查基金帐号状态出错. " + e);
            throw e;
        } finally {
            stmt.close();
        }
    }

    //检查基金状态
    public void checkFund() throws Exception {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int count = 0;
        try {
            logger.info("检查基金状态是否允许执行...");
            stmt = conn.prepareStatement(""
                    + "select fundStatus "
                    + "from ServiceFund "
                    + "where serviceCode = ? ");
            stmt.setString(1, serviceCode);
            rs = stmt.executeQuery();
            if (!rs.next()) {
                throw new AppException("当前基金状态不允许执行");
            }
            String fundStatusAllow = rs.getString(1);
            stmt.close();

            stmt = conn.prepareStatement(""
                    + "select count(*) "
                    + "from dual "
                    + "where ? in (" + fundStatusAllow + ")");
            stmt.setString(1, fundStatus);
            rs = stmt.executeQuery();
            rs.next();
            count = rs.getInt(1);
            if (count <= 0) {
                throw new AppException("当前基金状态不允许执行");
            }
            logger.debug("检查基金状态成功.");
        } catch (Exception e) {
            logger.error("检查基金状态出错. " + e);
            throw e;
        } finally {
            stmt.close();
        }
    }
}
